package com.thinksns.sociax.t4.android.verify;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckedTextView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.model.VerifyModel;
import com.thinksns.sociax.t4.unit.UnitSociax;

import java.util.ArrayList;
import java.util.List;

public class VerifyOtherPopupWindow extends PopupWindow {
    private List<VerifyModel> mData;
    private FrameLayout mContentView;
    private List<CheckedTextView> mCheckedTextViews;
    private Context mContext;
    private int mPadding;
    private AdapterView.OnItemClickListener mOnItemClickListener;
    private LinearLayout.LayoutParams mChildParams;
    private int mSelectPos = -1;

    public VerifyOtherPopupWindow(Context context, List<VerifyModel> data) {
        super(context);
        mContext = context;
        mData = data;
        mCheckedTextViews = new ArrayList<>();
        mPadding = UnitSociax.dip2px(mContext, 15);
        mChildParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        initView();
        setContentView(mContentView);
        setWidth(LayoutParams.MATCH_PARENT);
        setHeight(LayoutParams.MATCH_PARENT);
        setFocusable(true);
        setOutsideTouchable(true);
        setAnimationStyle(R.style.filter_window_animstyle);
        setBackgroundDrawable(new ColorDrawable());

    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    private void initView() {
        mContentView = new FrameLayout(mContext);
        mContentView.setBackgroundColor(Color.parseColor("#70000000"));
        LinearLayout contentView = new LinearLayout(mContext);
        contentView.setLayoutParams(mChildParams);
        contentView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
        contentView.setOrientation(LinearLayout.VERTICAL);
        contentView.setPadding(UnitSociax.dip2px(mContext, 14), 0, 0, 0);
        contentView.setDividerDrawable(ContextCompat.getDrawable(mContext, R.drawable.line_space_gray_1px));
        contentView.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE | LinearLayout.SHOW_DIVIDER_BEGINNING);
        contentView.removeAllViews();
        mCheckedTextViews.clear();
        int length = mData.size();
        for (int i = 0; i < length; i++) {
            contentView.addView(generateCheckedTextView(i, mData.get(i)));
        }
        mContentView.addView(contentView);
    }

    private CheckedTextView generateCheckedTextView(final int i, final VerifyModel item) {
        CheckedTextView textView = new CheckedTextView(mContext);
        textView.setLayoutParams(mChildParams);
        textView.setTextSize(15);
        textView.setTextColor(Color.parseColor("#272727"));
        textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
        textView.setPadding(0, mPadding, mPadding, mPadding);
        textView.setTag(item);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkedItem(i);
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(null, v, i, i);
                }
                dismiss();
            }
        });
        textView.setText(item.getTitle());
        mCheckedTextViews.add(textView);
        return textView;
    }

    private void checkedItem(int i) {
        for (CheckedTextView textView : mCheckedTextViews) {
            textView.setChecked(false);
            textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        }
        if (i < mData.size()) {
            mSelectPos = i;
            mCheckedTextViews.get(i).setChecked(true);
            mCheckedTextViews.get(i).setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_choose, 0);
        }
    }

    public void showAsDrop(View view) {
        showAsDropDown(view);
    }

    public boolean isSelect() {
        return mSelectPos != -1;
    }

    public void setSelectPos(int selectPos) {
        checkedItem(selectPos);
    }
}
